<?php

  class RSS extends Base {

    /** @var string The XML version of the RSS feed. */
    private $xml_version = "1.0";

    /** @var string The RSS version of the feel. */
    private $rss_version = "2.0";

    /** @var string The encoding version of the XML. */
    private $encoding = "iso-8859-1";

    /** @var string The title for the RSS feed. */
    private $title = "";

    /** @var string The link of the RSS feed. */
    private $link = "";

    /** @var string The description of the RSS feed. */
    private $description = "";

    /** @var string The language of the RSS feed. */
    private $language = "";

    /** @var string The copyright of the RSS feed. */
    private $copyright = "";

    /** @var string The editor of the RSS feed. */
    private $editor = "";

    /** @var string The webmaster of the RSS feed. */
    private $webmaster = "";

    /** @var string The publication date of the RSS feed. */
    private $publication_date = "";

    /** @var string The last publication date of the RSS feed. */
    private $last_publication_date = "";

    /** @var string The category of the RSS feeD. */
    private $category = "";

    /** @var string The generator of the RSS feed. */
    private $generator = "";

    /** @var string The URL of the format documentation of the RSS feed. */
    private $docs = "";

    /** @var integer The time to live, used to define how long the RSS feed can be cached before a refresh from the source. */
    private $ttl = 0;

    /** @var string The image (GIF, JPG or PNG) to be displayed with the RSS feed. */
    private $image = "";

    /** @var integer The pics rating for the RSS feed. */
    private $rating = 0;

    /** @var string The text input box that can be displayed with the RSS feed. */
    private $text_input = "";

    /** @@var string A hint for aggregators telling them which hours they can skip. */
    private $skip_hours = "";

    /** @var string A hint for aggregators telling them which days they can skip. */
    private $skip_days = "";

    /** @var array Public collection of items conteined in the main RSS feed. */
    public $Items = array();

    /**
     * Store a title, a link and a description in private to create the RSS feed.<br />
     *
     * @param string $title The title of the RSS feed.
     * @param string $link The URL to the RSS feed.
     * @param string $description The description of the RSS feed.
     *
     * @see <a href="http://fr2.php.net/manual/en/function.is-string.php">is_string()</a>
     */
    public function __construct($title = "title", $link = "link", $description = "description") {
      if (is_string($title))
        if (is_string($link))
          if (is_string($description)) {
            $this->title = $title;
            $this->link = $link;
            $this->description = $description;
          }
          else
            throw new Exception("\$description parameter should be a string", 0);
        else
          throw new Exception("\$link parameter should be a string", 0);
      else
        throw new Exception("\$title parameter should be a string", 0);
    }

    /**
     * Get the XML version of the RSS feed.<br />
     *
     * @return string Returns the XML version.
     */
    public function getXmlVersion() {
      return $this->xml_version;
    }

    /**
     * Set the XML version of the RSS feed.<br />
     *
     * @param string $xml_version The XML version.
     *
     * @see <a href="http://fr2.php.net/manual/en/function.is-string.php">is_string()</a>
     */
    public function setXmlVersion($xml_version) {
      if (is_string($xml_version))
        $this->xml_version = $xml_version;
      else
        throw new Exception("\$xml_version parameter should be a string", 0);
    }

    /**
     * Get the RSS version of the RSS feed.<br />
     *
     * @return string Returns the RSS version.
     */
    public function getRssVersion() {
      return $this->rss_version;
    }

    /**
     * Set the RSS version of the RSS feed.<br />
     *
     * @param string $rss_version The RSS version.
     *
     * @see <a href="http://fr2.php.net/manual/en/function.is-string.php">is_string()</a>
     */
    public function setRssVersion($rss_version) {
      if (is_string($rss_version))
        $this->rss_version = $rss_version;
      else
        throw new Exception("\$rss_version parameter should be a string", 0);
    }

    /**
     * Get the encoding of the RSS feed.<br />
     *
     * @return string Returns the encoding.
     */
    public function getEncoding() {
      return $this->encoding;
    }

    /**
     * Set the encoding of the RSS feed.<br />
     *
     * @param string $encoding The encoding.
     *
     * @see <a href="http://fr2.php.net/manual/en/function.is-string.php">is_string()</a>
     */
    public function setEncoding($encoding) {
      if (is_string($encoding))
        $this->encoding = $encoding;
      else
        throw new Exception("\$encoding parameter should be a string", 0);
    }

    /**
     * Get the title of the RSS feed.<br />
     *
     * @return string Returns the title.
     */
    public function getTitle() {
      return $this->title;
    }

    /**
     * Set the title of the RSS feed.<br />
     *
     * @param string $title The title.
     *
     * @see <a href="http://fr2.php.net/manual/en/function.is-string.php">is_string()</a>
     */
    public function setTitle($title) {
      if (is_string($title))
        $this->title = $title;
      else
        throw new Exception("\$title parameter should be a string", 0);
    }

    /**
     * Get The URL of the RSS feed.<br />
     *
     * @return string Returns the URL.
     */
    public function getLink() {
      return $this->link;
    }

    /**
     * Set the URL of the RSS feed.<br />
     *
     * @param string $link The URL.
     *
     * @see <a href="http://fr2.php.net/manual/en/function.is-string.php">is_string()</a>
     */
    public function setLink($link) {
      if (is_string($link))
        $this->link = $link;
      else
        throw new Exception("\$link parameter should be a string", 0);
    }

    /**
     * Get the description of the RSS feed.<br />
     *
     * @return string Returns the description.
     */
    public function getDescription() {
      return $this->description;
    }

    /**
     * Set the description of the RSS feed.<br />
     *
     * @param string $description The description.
     *
     * @see <a href="http://fr2.php.net/manual/en/function.is-string.php">is_string()</a>
     */
    public function setDescription($description) {
      if (is_string($description))
        $this->description = $description;
      else
        throw new Exception("\$description parameter should be a string", 0);
    }

    /**
     * Get the language of the RSS feed.<br />
     *
     * @return string Returns the language.
     */
    public function getLanguage() {
      return $this->language;
    }

    /**
     * Set the language of the RSS feed.<br />
     *
     * @param string $language The language.
     *
     * @see <a href="http://fr2.php.net/manual/en/function.is-string.php">is_string()</a>
     */
    public function setLanguage($language) {
      if (is_string($language))
        $this->language = $language;
      else
        throw new Exception("\$language parameter should be a string", 0);
    }

    /**
     * Get the copyright of the RSS feed.<br />
     *
     * @return string Returns the copyright.
     */
    public function getCopyright() {
      return $this->copyright;
    }

    /**
     * Set the copyright of the RSS feed.<br />
     *
     * @param string $copyright The copyright.
     *
     * @see <a href="http://fr2.php.net/manual/en/function.is-string.php">is_string()</a>
     */
    public function setCopyright($copyright) {
      if (is_string($copyright))
        $this->copyright = $copyright;
      else
        throw new Exception("\$copyright parameter should be a string", 0);
    }

    /**
     * Get the editor of the RSS feed.<br />
     *
     * @return string Returns the editor.
     */
    public function getEditor() {
      return $this->editor;
    }

    /**
     * Set the editor of the RSS feed.<br />
     *
     * @param string $editor The editor.
     *
     * @see <a href="http://fr2.php.net/manual/en/function.is-string.php">is_string()</a>
     */
    public function setEditor($editor) {
      if (is_string($editor))
        $this->editor = $editor;
      else
        throw new Exception("\$editor parameter should be a string", 0);
    }

    /**
     * Get the webmaster of the RSS feed.<br />
     *
     * @return string Returns the webmaster.
     */
    public function getWebmaster() {
      return $this->webmaster;
    }

    /**
     * Set the webmaster of the RSS feed.<br />
     *
     * @param string $webmaster The webmaster.
     *
     * @see <a href="http://fr2.php.net/manual/en/function.is-string.php">is_string()</a>
     */
    public function setWebmaster($webmaster) {
      if (is_string($webmaster))
        $this->webmaster = $webmaster;
      else
        throw new Exception("\$webmaster parameter should be a string", 0);
    }

    /**
     * Get the publication date of the RSS feed.<br />
     *
     * @return string Returns the publication date.
     */
    public function getPublicationDate() {
      return $this->publication_date;
    }

    /**
     * Set the publication date of the RSS feed.<br />
     *
     * @param string $publication_date The publication date.
     *
     * @see <a href="http://fr2.php.net/manual/en/function.is-string.php">is_string()</a>
     */
    public function setPublicationDate($publication_date) {
      if (is_string($publication_date))
        $this->publication_date = $publication_date;
      else
        throw new Exception("\$publication_date parameter should be a string", 0);
    }

    /**
     * Get the last publication date of the RSS feed.<br />
     *
     * @return string Returns the last publication date.
     */
    public function getLastPublicationDate() {
      return $this->last_publication_date;
    }

    /**
     * Set the last publication date of the RSS feed.<br />
     *
     * @param string $last_publication_date The last publication date.
     *
     * @see <a href="http://fr2.php.net/manual/en/function.is-string.php">is_string()</a>
     */
    public function setLastPublicationDate($last_publication_date) {
      if (is_string($last_publication_date))
        $this->last_publication_date = $last_publication_date;
      else
        throw new Exception("\$last_publication parameter should be a string", 0);
    }

    /**
     * Get the category of the RSS feed.<br />
     *
     * @return string Returns the category.
     */
    public function getCategory() {
      return $this->category;
    }

    /**
     * Set the category of the RSS feed.<br />
     *
     * @param string $category The category.
     *
     * @see <a href="http://fr2.php.net/manual/en/function.is-string.php">is_string()</a>
     */
    public function setCategory($category) {
      if (is_string($category))
        $this->category = $category;
      else
        throw new Exception("\$category parameter should be a string", 0);
    }

    /**
     * Get the generator of the RSS feed.<br />
     *
     * @return string Returns the generator.
     */
    public function getGenerator() {
      return $this->generator;
    }

    /**
     * Set the generator of the RSS feed.<br />
     *
     * @param string $generator The generator.
     *
     * @see <a href="http://fr2.php.net/manual/en/function.is-string.php">is_string()</a>
     */
    public function setGenerator($generator) {
      if (is_string($generator))
        $this->generator = $generator;
      else
        throw new Exception("\$generator parameter should be a string", 0);
    }

    /**
     * Get the format documentation URL of the RSS feed.<br />
     *
     * @return string Returns the format documention URL.
     */
    public function getDocs() {
      return $this->docs;
    }

    /**
     * Set the format documentation URL of the RSS feed.<br />
     *
     * @param string $docs The format documentation URL.
     *
     * @see <a href="http://fr2.php.net/manual/en/function.is-string.php">is_string()</a>
     */
    public function setDocs($docs) {
      if (is_string($docs))
        $this->docs = $docs;
      else
        throw new Exception("\$docs parameter should be a string", 0);
    }

    /**
     * Get the time to live before refresh of the RSS feed before.<br />
     *
     * @return integer Returns the time to live.
     */
    public function getTTL() {
      return $this->ttl;
    }

    /**
     * Set the time to live of the RSS feed.<br />
     *
     * @param integer $ttl The time to live.
     *
     * @see <a href="http://fr2.php.net/manual/en/function.is-int.php">is_int()</a>
     */
    public function setTTL($ttl) {
      if (is_int($ttl))
        $this->ttl = $ttl;
      else
        throw new Exception("\$ttl parameter should be an integer", 0);
    }

    /**
     * Get the image URL of the RSS feed.<br />
     *
     * @return string Returns the image URL.
     */
    public function getImage() {
      return $this->image;
    }

    /**
     * Set the image URL of the RSS feed.<br />
     *
     * @param string $image The image URL.
     *
     * @see <a href="http://fr2.php.net/manual/en/function.is-string.php">is_string()</a>
     */
    public function setImage($image) {
      if (is_string($image))
        $this->image = $image;
      else
        throw new Exception("\$image parameter should be a string", 0);
    }

    /**
     * Get the rating of the pic of the RSS feed.<br />
     *
     * @return integer Returns the rating of the pic.
     */
    public function getRating() {
      return $this->rating;
    }

    /**
     * Set the rating of the pic of the RSS feed.<br />
     *
     * @param integer $rating The rating of the pic.
     *
     * @see <a href="http://fr2.php.net/manual/en/function.is-int.php">is_int()</a>
     */
    public function setRating($rating) {
      if (is_int($rating))
        $this->rating = $rating;
      else
        throw new Exception("\$rating parameter should be an integer", 0);
    }

    /**
     * Get the input text of the RSS feed.<br />
     *
     * @return string Returns the input text.
     */
    public function getTextInput() {
      return $this->text_input;
    }

    /**
     * Set the input text of the RSS feed.<br />
     *
     * @param string $text_input The input text.
     *
     * @see <a href="http://fr2.php.net/manual/en/function.is-string.php">is_string()</a>
     */
    public function setTextInput($text_input) {
      if (is_string($text_input))
        $this->text_input = $text_input;
      else
        throw new Exception("\$text_input parameter should be a string", 0);
    }

    /**
     * Get the skip hours of the RSS feed.<br />
     *
     * @return string Returns the skip hours.
     */
    public function getSkipHours() {
      return $this->skip_hours;
    }

    /**
     * Set the skip hours of the RSS feed.<br />
     *
     * @param string $skip_hours The skip hours.
     *
     * @see <a href="http://fr2.php.net/manual/en/function.is-string.php">is_string()</a>
     */
    public function setSkipHours($skip_hours) {
      if (is_string($skip_hours))
        $this->skip_hours = $skip_hours;
      else
        throw new Exception("\$skip_hours parameter should be a string", 0);
    }

    /**
     * Get the skip days of the RSS feed.<br />
     *
     * @return string Returns the skip days.
     */
    public function getSkipDays() {
      return $this->skip_days;
    }

    /**
     * Set the skip days of the RSS feed.<br />
     *
     * @param string $skip_days The skip days.
     *
     * @see <a href="http://fr2.php.net/manual/en/function.is-string.php">is_string()</a>
     */
    public function setSkipDays($skip_days) {
      if (is_string($skip_days))
        $this->skip_days = $skip_days;
      else
        throw new Exception("\$skip_days parameter should be a string", 0);
    }

    /**
     * Add a RSS item to the inner collection of items.<br />
     *
     * @param RSSItem $Item The item to push in the collection.
     *
     * @see <a href="http://fr2.php.net/manual/en/function.is-a.php">is_a()</a>
     * @see <a href="http://fr2.php.net/manual/en/function.array-push.php">array_push()</a>
     */
    public function AddItem($Item) {
      if (is_a($Item, "RSSItem"))
        return array_push($this->Items, $Item);
    }

    /**
     * Create the file for the RSS feed.<br />
     *
     * @param string $filename The name for the RSS file.
     *
     * @see <a href="http://fr2.php.net/manual/en/function.is-string.php">is_string()</a>
     */
    public function MakeFile($filename) {
      if (is_string($filename)) {
        $stream = "<?xml version=\"" . $this->xml_version . "\" encoding=\"" . $this->encoding . "\" ?>\r\n" .
                "  <rss version=\"" . $this->rss_version . "\">\r\n" .
                "    <channel>\r\n";

        $stream .= "      <title>" . $this->title . "</title>\r\n" .
                "      <link>" . $this->link . "</link>\r\n" .
                "      <description>" . $this->description . "</description>\r\n";

        if (!empty($this->language))
          $stream .= "      <language>" . $this->language . "</language>\r\n";

        if (!empty($this->copyright))
          $stream .= "      <copyright>" . $this->copyright . "</copyright>\r\n";

        if (!empty($this->editor))
          $stream .= "      <managingEditor>" . $this->editor . "</managingEditor>\r\n";

        if (!empty($this->webmaster))
          $stream .= "      <webMaster>" . $this->webmaster . "</webMaster>\r\n";

        if (!empty($this->publication_date))
          $stream .= "      <pubDate>" . $this->publication_date . "</pubDate>\r\n";

        if (!empty($this->last_publication_date))
          $stream .= "      <lastBuildDate>" . $this->last_publication_date . "</lastBuildDate>\r\n";

        if (!empty($this->category))
          $stream .= "      <category>" . $this->category . "</category>\r\n";

        if (!empty($this->generator))
          $stream .= "      <generator>" . $this->generator . "</generator>\r\n";

        if (!empty($this->docs))
          $stream .= "      <docs>" . $this->docs . "</docs>\r\n";

        if (!empty($this->cloud))
          $stream .= "      <cloud>" . $this->cloud . "</cloud>\r\n";

        if (!empty($this->ttl))
          $stream .= "      <ttl>" . $this->ttl . "</ttl>\r\n";

        if (!empty($this->image))
          $stream .= "      <image>" . $this->image . "</image>\r\n";

        if (!empty($this->rating))
          $stream .= "      <rating>" . $this->rating . "</rating>\r\n";

        if (!empty($this->text_input))
          $stream .= "      <textInput>" . $this->text_input . "</textInput>\r\n";

        if (!empty($this->skip_hours))
          $stream .= "      <skipHours>" . $this->skip_hours . "</skipHours>\r\n";

        if (!empty($this->skip_days))
          $stream .= "      <skipDays>" . $this->skip_days . "</skipDays>\r\n";

        $stream .= "    </channel>\r\n";

        for ($i = 0; $i < count($this->Items); ++$i)
          $stream .= $this->Items[$i]->GetItem();

        $stream .= "  </rss>";

        $fp = fopen($filename, "w");

        fwrite($fp, $stream);

        return fclose($fp);
      }
    }

  }

?>
